<template>
    <div class="user-list">
        <h3>用户列表</h3>
        <!-- :data="filterTableData" -->
        <el-table :data="tableData" style="width: 100%">
            <el-table-column label="ID" prop="id" />
            <el-table-column label="昵称" prop="user_nick_name" />
            <el-table-column label="账号" prop="user_acc" />
            <el-table-column label="头像">
                <template #default="scope">
                    <el-image class="table-td-thumb" :src="scope.row.user_picUrl" 
                        :z-index="10" :preview-src-list="[scope.row.user_picUrl]"
                        preview-teleported>
                    </el-image>
                </template>
            </el-table-column>
            <el-table-column label="地址" prop="user_address" />
            <el-table-column label="性别" prop="user_sex" />
            <el-table-column label="年龄" prop="user_age" />
            <!-- <el-table-column label="日期" prop="user_create_time" /> -->
            <el-table-column align="right">
                <template #header>
                    <el-input v-model="search" size="small" placeholder="搜索" />
                </template>
                <template #default="scope">
                    <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <div class="page">
            <!-- :page-count="page" -->
            <el-pagination
            :page-size="pageSize"
           
            :pager-count="pageSize"
            layout="prev, pager, next"
            :total="totalNum"
            @change="changePageFn"
        />
        </div>
    </div>
</template>

<script lang="ts" setup>
import { computed, ref } from 'vue'
import { userList,deleteUserFunc } from '../../api/request'
import { User } from '@element-plus/icons-vue';
import { ElMessage, ElMessageBox } from 'element-plus';
// 定义接口，形容对象的结构
interface User {
    id?:number|string
    user_id:number
    user_nick_name: string
    user_acc: string
    user_picUrl: string
    user_address: string
    user_sex: string
    user_age: number
    user_create_time: string
}

// 总数
let totalNum = ref(0)
let page = ref(1)
let pageSize = ref(5)

const search = ref('')
const handleEdit = (index: number, row: User) => {
    // console.log(index, row)
}
const handleDelete = (index: number, row: User) => {
    // console.log(index, row)
    let {user_id,user_nick_name} = row;
    // 二次确认删除
    ElMessageBox.confirm(`确定要删除“${user_nick_name}”吗？`, '提示', {
        type: 'warning'
    })
    .then(() => {
        // 删除数据
        deleteUserFunc({user_id})
        .then(
            (content: any)=>{
                // console.log(222);
                ElMessage.success(content.data.msg);
                tableData.value.splice(index, 1);
                // 重新初始化
                userListFn();
            },
            err=>{
                // console.log(111)
                ElMessage.error(err.msg);
            }
        )
       
      
    })
    .catch(() => { });
}

// 用户列表数据
const tableData: User[] = ref([])


// 定义函数获取用户列表
const userListFn = async (page=1,pageSize=5)=> {
    // let token = localStorage.getItem("TOKEN")
    await userList({page,pageSize}).then(
        (content:any)=>{
            // console.log(content)
            let {result, code,total} = content.data;
            // console.log(result,code)
            if(code == 200) {
                tableData.value = result;
                totalNum.value = total;
            }
        }
    )
}

// 点击分页时执行的函数
const changePageFn = (num:number) => {
    // console.log(value)
    page.value = num;
    userListFn(page.value, pageSize.value);
}

// 
userListFn(page.value, pageSize.value);

</script>

<style lang="scss">
.user-list {
    h3 {
        margin-bottom: 15px;
    }
}
</style>